// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
	let openid = event.userInfo.openId
	let user = await db.collection('userInfo').where({
		_openid: openid
	}).get()
	if (!user.data.length) {
		try {
			return await db.collection('userInfo').add({
				data: {
					nickName: event.nickName,
					avatarUrl: event.avatarUrl,
					phone: event.phone,
					_openid: openid,
					ctime: Date.parse(new Date()),
				}
			})
		} catch (e) {
			return '数据存储失败'
		}
		return '新用户'
	} else {
		return user
	}

}